home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Palettes / Clocks / CalendarClock.m < prev    next >
Text File  |  1995-06-12  |  9KB  |  358 lines

  1. //----------------------------------------------------------------------------------------------------
  2. //
  3. //    CalendarClock
  4. //
  5. //    Inherits From:        Clock
  6. //
  7. //    Declared In:        CalendarClock.h
  8. //
  9. //    Disclaimer
  10. //
  11. //        You may freely copy, distribute and reuse this software and its
  12. //        associated documentation. I disclaim any warranty of any kind, 
  13. //        expressed or implied, as to its fitness for any particular use.
  14. //
  15. //----------------------------------------------------------------------------------------------------
  16. #import "CalendarClock.h"
  17.  
  18.  
  19. #define TIMESTRING_OFFSET        8.0
  20. #define DAYSTRING_OFFSET        3.0
  21. #define DATESTRING_OFFSET        28.0
  22. #define MONTHSTRING_OFFSET    27.0
  23.  
  24. static const char*  DAYS [7] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
  25. static const char*  MONTHS [12] = 
  26. { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
  27.  
  28. static  id timeFont;
  29. static  id dayOfWeekFont;
  30. static  id dateFont;
  31. static  id monthFont;
  32.  
  33.  
  34. @implementation CalendarClock
  35.  
  36. //----------------------------------------------------------------------------------------------------
  37. //    Private Methods
  38. //----------------------------------------------------------------------------------------------------
  39. - (NXPoint) _locationForString: (STR)aString usingFont: aFont
  40. {
  41.     NXPoint    stringLocation;
  42.  
  43.     stringLocation.x = ((NX_WIDTH(&bounds) - [aFont getWidthOf:aString]) / 2.0);
  44.     stringLocation.y = ((NX_HEIGHT(&bounds) / 2.0) + ([aFont pointSize] / 2.0));
  45.  
  46.     return stringLocation;
  47. }
  48.  
  49.  
  50. - _drawDayOfWeek
  51. {
  52.         char     stringBuffer[5];
  53.     NXPoint    stringLocation;
  54.     
  55.     sprintf (stringBuffer,"%s", DAYS[WEEKDAY(displayTimeAndDate)]);
  56.     stringLocation = [self _locationForString:stringBuffer usingFont: dayOfWeekFont];
  57.     NXSetColor (weekdayColor);    
  58.     [dayOfWeekFont set];
  59.     PSmoveto (stringLocation.x, stringLocation.y - DAYSTRING_OFFSET);
  60.     PSshow (stringBuffer);
  61.     return self;
  62. }
  63.     
  64.  
  65. - _drawDate
  66. {
  67.         char     stringBuffer[5];
  68.     NXPoint    stringLocation;
  69.     
  70.     sprintf (stringBuffer,"%d", DAY(displayTimeAndDate));
  71.     stringLocation = [self _locationForString:stringBuffer usingFont: dateFont];
  72.     NXSetColor (dateColor);    
  73.     [dateFont set];
  74.     PSmoveto (stringLocation.x, stringLocation.y - DATESTRING_OFFSET);
  75.     PSshow (stringBuffer);
  76.     return self;
  77. }
  78.  
  79.  
  80. - _drawMonth
  81. {
  82.         char     stringBuffer[10];
  83.     NXPoint    stringLocation;
  84.     
  85.     if (wantsYear)
  86.         sprintf (stringBuffer,"%s%.2d", 
  87.             MONTHS[MONTH(displayTimeAndDate)], YEAR(displayTimeAndDate));
  88.     else
  89.         sprintf (stringBuffer,"%s", MONTHS[MONTH(displayTimeAndDate)]);
  90.     
  91.     stringLocation = [self _locationForString:stringBuffer usingFont: monthFont];
  92.     NXSetColor (monthColor);    
  93.     [monthFont set];
  94.     PSmoveto (stringLocation.x, stringLocation.y - MONTHSTRING_OFFSET);
  95.     PSshow (stringBuffer);
  96.     return self;
  97. }
  98.  
  99.  
  100. //----------------------------------------------------------------------------------------------------
  101. //    Initialization and Freeing
  102. //----------------------------------------------------------------------------------------------------
  103. + initialize
  104. {
  105.     timeFont = [Font newFont:"Times-Italic" size:14 style:0 matrix:NX_IDENTITYMATRIX];
  106.     dayOfWeekFont = [Font newFont:"Helvetica" size:8 style:0 matrix:NX_IDENTITYMATRIX];
  107.     dateFont = [Font newFont:"Times-Roman" size:24 style:0 matrix:NX_IDENTITYMATRIX];
  108.     monthFont = [Font newFont:"Helvetica-Oblique" size:9 style:0 matrix:NX_IDENTITYMATRIX];
  109.     return self;
  110. }
  111.  
  112.  
  113. - initFrame: (const NXRect *)aRect
  114. {
  115.     //  Give default characteristics.  Fix size to 64 x 64. 
  116.     
  117.     NXRect    frameRect;
  118.     NXSetRect (&frameRect, NX_X(aRect), NX_Y(aRect), 64.0, 64.0);
  119.     [super initFrame:&frameRect];
  120.     
  121.     [self clockFace: [NXImage findImageNamed: "CalendarFace"]];
  122.     [self timeColor: NX_COLORGREEN];
  123.     [self weekdayColor: NX_COLORBLACK];
  124.     [self dateColor: NX_COLORBLACK];
  125.     [self monthColor: NX_COLORRED];
  126.     [self wantsDate: YES];
  127.     [self wantsYear: NO];
  128.     
  129.     return self;
  130. }
  131.  
  132.  
  133. //----------------------------------------------------------------------------------------------------
  134. //    Accessing Display Colors
  135. //----------------------------------------------------------------------------------------------------
  136. - timeColor: (NXColor) aColor
  137. {
  138.     timeColor = aColor;
  139.     [self update];
  140.     return self;
  141. }
  142.  
  143.  
  144. - weekdayColor: (NXColor) aColor;
  145. {
  146.     weekdayColor = aColor;
  147.     [self update];
  148.     return self;
  149. }
  150.  
  151.  
  152. - dateColor: (NXColor) aColor;
  153. {
  154.     dateColor = aColor;
  155.     [self update];
  156.     return self;
  157. }
  158.  
  159.  
  160. - monthColor: (NXColor) aColor;
  161. {
  162.     monthColor = aColor;
  163.     [self update];
  164.     return self;
  165. }
  166.  
  167.  
  168. - (NXColor) timeColor
  169. {
  170.     return timeColor;
  171. }
  172.  
  173.  
  174. - (NXColor) weekdayColor;
  175. {
  176.     return weekdayColor;
  177. }
  178.  
  179.  
  180. - (NXColor) dateColor;
  181. {
  182.     return dateColor;
  183. }
  184.  
  185.  
  186. - (NXColor) monthColor;
  187. {
  188.     return monthColor;
  189. }
  190.  
  191.  
  192. //----------------------------------------------------------------------------------------------------
  193. //    Enabling Year Display
  194. //-----------------------------------------------------------------------------------------------------
  195. - wantsYear: (BOOL) aFlag
  196. {
  197.     wantsYear = aFlag;
  198.     [self update];
  199.     return self;
  200. }
  201.  
  202.  
  203. - (BOOL) wantsYear
  204. {
  205.     return wantsYear;
  206. }
  207.  
  208.  
  209. //----------------------------------------------------------------------------------------------------
  210. //    Action Methods
  211. //----------------------------------------------------------------------------------------------------
  212. - takeWantsYearFlagFrom :sender
  213. {
  214.     if ([sender isKindOf: [Matrix class]])
  215.         sender = [sender selectedCell];
  216.         
  217.     if ([sender respondsTo: @selector (state)])
  218.         [self wantsYear: [sender state]];
  219.         
  220.     return self;
  221. }
  222.  
  223.  
  224. - takeTimeColorFrom: sender
  225. {
  226.     if ([sender respondsTo: @selector (color)]) [self timeColor: [sender color]];
  227.     return self;
  228. }
  229.  
  230.  
  231. - takeWeekdayColorFrom: sender
  232. {
  233.     if ([sender respondsTo: @selector (color)]) [self weekdayColor: [sender color]];
  234.     return self;
  235. }
  236.  
  237.  
  238. - takeDateColorFrom: sender
  239. {
  240.     if ([sender respondsTo: @selector (color)]) [self dateColor: [sender color]];
  241.     return self;
  242. }
  243.  
  244.  
  245. - takeMonthColorFrom: sender
  246. {
  247.     if ([sender respondsTo: @selector (color)]) [self monthColor: [sender color]];
  248.     return self;
  249. }
  250.  
  251.  
  252. //-----------------------------------------------------------------------------------------------------
  253. //    Draw Methods
  254. //-----------------------------------------------------------------------------------------------------
  255. - drawTime
  256. {
  257.         int         hour = HOUR(displayTimeAndDate);
  258.         char     stringBuffer[10];
  259.     NXPoint    stringLocation;
  260.     
  261.     if (! flags.wantsMilitaryTime)
  262.         {
  263.             hour = fmod (HOUR(displayTimeAndDate),12);
  264.             if (! hour) hour = 12;
  265.         }
  266.         
  267.     if (flags.wantsMilitaryTime)
  268.         sprintf (stringBuffer, "%.2d:%.2d", hour, MINUTE(displayTimeAndDate));
  269.     else
  270.         sprintf (stringBuffer, "%d:%.2d", hour, MINUTE(displayTimeAndDate));
  271.  
  272.         if (flags.wantsSeconds)
  273.         sprintf (stringBuffer, "%s:%.2d", stringBuffer, SECOND(displayTimeAndDate));
  274.     else
  275.         if (! flags.wantsMilitaryTime)
  276.              sprintf (stringBuffer, "%s %s", 
  277.                 stringBuffer, 
  278.                 (HOUR(displayTimeAndDate) < 12 ? "am" : "pm"));
  279.   
  280.     stringLocation = [self _locationForString:stringBuffer usingFont: timeFont];
  281.  
  282.     [timeFont set];
  283.     NXSetColor (timeColor);    
  284.     PSmoveto (stringLocation.x, stringLocation.y + TIMESTRING_OFFSET);
  285.     PSshow (stringBuffer);
  286.  
  287.     return self;
  288. }
  289.  
  290.  
  291. - drawDate
  292. {
  293.     [self _drawDayOfWeek];
  294.     [self _drawDate];
  295.     [self _drawMonth];
  296.     return self;
  297. }
  298.  
  299.  
  300. //----------------------------------------------------------------------------------------------------
  301. //    Archiving Methods
  302. //----------------------------------------------------------------------------------------------------
  303. - read: (NXTypedStream*) stream
  304. {
  305.     [super read: stream];
  306.     timeColor = NXReadColor (stream);
  307.     weekdayColor = NXReadColor (stream);
  308.     dateColor = NXReadColor (stream);
  309.     monthColor = NXReadColor (stream);
  310.     NXReadTypes (stream, "c", &wantsYear);
  311.     return self;
  312. }
  313.  
  314.  
  315. - write: (NXTypedStream*) stream
  316. {
  317.     [super write: stream];
  318.     NXWriteColor (stream, timeColor);
  319.     NXWriteColor (stream, weekdayColor);
  320.     NXWriteColor (stream, dateColor);
  321.     NXWriteColor (stream, monthColor);
  322.     NXWriteTypes (stream, "c", &wantsYear);
  323.     return self;
  324. }
  325.  
  326.  
  327. //----------------------------------------------------------------------------------------------------
  328. //    Superclass Overrides
  329. //----------------------------------------------------------------------------------------------------
  330. - wantsDate: (BOOL) aFlag
  331. {
  332.     return [super wantsDate: YES];
  333. }
  334.  
  335.  
  336. //----------------------------------------------------------------------------------------------------
  337. //    IB Methods
  338. //----------------------------------------------------------------------------------------------------
  339. - getMinSize:(NXSize *)minSize maxSize:(NXSize *)maxSize from:(int)where
  340. {
  341.     //  Constrains resize within IB.
  342.     
  343.     minSize->width = maxSize->width = 64.0;
  344.     minSize->height = maxSize->height = 64.0;
  345.     return self;
  346. }
  347.  
  348.  
  349. - (const char*) getInspectorClassName
  350. {
  351.     //  Returns the name of the class responsible for managing custom 
  352.     //  IB Attributes inspection.
  353.         
  354.     return "CalendarClockInspector";
  355. }
  356.  
  357.  
  358. @end